package com.ruoyi.patrol.util;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * packageName com.ruoyi.patrol.util
 *
 * @author nan.li
 * @version JDK 8
 * @className DataUtil
 * @date 2024/3/4
 * @description TODO
 */
public class DataUtil {

    public static List<Map<String, Object>> locusDataCover(List<Map<String, Object>> in, Map<String, Object> addParam){
        List<Map<String, Object>> out = new ArrayList<>();
        for (Map<String, Object> map : in) {
            Map<String, Object> one = new HashMap<>();
            one.putAll(addParam);
            one.put("date", map.get("GpsTime"));
            List<Double> position = (List<Double>) map.get("position");
            one.put("lon", position.get(0));
            one.put("lat", position.get(1));
            out.add(one);
        }
        return out;
    }

    public static List<Map<String, Object>> locusDataUnCover(List<Map<String, Object>> in){
        Map<Long, List<Map<String, Object>>> workId = in.stream().collect(Collectors.groupingBy(x -> MapUtil.getLong(x, "work_id")));
        List<Map<String, Object>> out = workId.entrySet().stream().map(x -> {
            List<Map<String, Object>> list = x.getValue();
            Map<String, Object> map = new HashMap<>();
            map.put("workId", x.getKey());
            map.put("user", list.get(0).get("user"));
            map.put("workType", list.get(0).get("work_type"));
            JSONArray array = new JSONArray();
            for (Map<String, Object> y : list) {
                JSONObject entries = new JSONObject();
                entries.set("GpsTime", y.get("date"));
                entries.set("position", new String[]{MapUtil.getStr(y, "lon"), MapUtil.getStr(y, "lat")});
                array.add(entries);
            }
            map.put("data", array);
            return map;
        }).collect(Collectors.toList());
        return out;
    }
}